<template>
<el-dialog :title="dialogTitle" :visible.sync="visible" width="30%" center append-to-body>
    <div class="dialog-body">
        <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-position="top">
            <el-form-item label="备注" prop="remark">
                <el-input type="textarea" v-model="ruleForm.remark" placeholder="请填写备注"></el-input>
            </el-form-item>

            <el-form-item label="附件">
                <upload-files ref="uploadFiles" v-model="ruleForm.image"></upload-files>
            </el-form-item>
        </el-form>
    </div>
    <div slot="footer" class="dialog-footer">
        <el-button @click="closeDialog">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
    </div>
</el-dialog>
</template>

<script>
import uploadFiles from '@/components/upload/files.vue';

import {
    doCommentApi
} from '@/api/audit/index.js';

const ruleForm = {
    flow_id: '',
    remark: '',
    image: [],
};
export default {
    name: "AuditComment",
    components: {
        uploadFiles
    },
    data() {
        return {
            type: '',
            dialogTitle: '评论',
            visible: false,
            ruleForm: Object.assign({}, ruleForm),
            rules: {
                remark: [{
                    required: true,
                    message: "请填写备注",
                }],
            }
        };
    },
    methods: {
        showDialog(flowId) {
            this.ruleForm = Object.assign({}, ruleForm);
            this.ruleForm.image = [];
            this.ruleForm.flow_id = flowId;
            this.visible = true;
            
            this.$nextTick(() => {
                this.$refs.uploadFiles.clearFiles();
            })
        },
        closeDialog() {
            this.visible = false;
        },
        submit() {
            let _this = this;
            this.$refs["ruleForm"].validate((valid) => {
                if (!valid) {
                    return false;
                }

                let data = Object.assign({}, _this.ruleForm);
                doCommentApi(data).then((res) => {
                    if (res.code == "200") {
                        _this.$message.success(res.msg);
                        this.$refs.uploadFiles.clearFiles();
                        _this.closeDialog();
                        _this.$emit("doRefresh");
                    } else {
                        _this.$message.warning(res.msg);
                    }
                });

            });
        }
    }
}
</script>
